﻿using IPC.Communication;
using IPC.Communication.Authorization;
using IPC.Communication.Core.Types;
using IPC.Communication.Profinet.AllenBradley;
using IPC.Communication.Profinet.Siemens;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static MaterialDesignThemes.Wpf.Theme.ToolBar;

namespace IPCTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        Type DataType = Type.None;

        AllenBradleyNet? allenBradleyNet;

        SiemensClient siemensClient;
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string ip = this.textBox.Text;
                allenBradleyNet = new AllenBradleyNet(ip);
                OperateResult operateResult = allenBradleyNet.ConnectServer();
                if (operateResult.IsSuccess)
                {
                    this.label.Content = "连接成功";
                    MessageBox.Show("连接成功");
                }
                else
                {
                    this.label.Content = $"连接失败:{operateResult.Message}";
                    MessageBox.Show($"连接失败:{operateResult.Message}");
                }
            }
            catch(Exception ex)
            {
                this.label.Content = ex.Message;
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            richTextBox.Document.Blocks.Clear();
            if (allenBradleyNet == null)
            {
                return;
            }
            string address = txtPoint.Text;
            Console.WriteLine(address);
            ushort length = ushort.Parse(txtLength.Text);
            switch (DataType)
            {
                case Type.None:  break;
                case Type.Int:
                    OperateResult<int[]> operateResultInt = allenBradleyNet.ReadInt32(address, length);
                    foreach (int item in operateResultInt.Content)
                    {
                        SetValue(item);
                    }
                    break;
                case Type.Short:
                    try
                    {
                        Console.WriteLine("address:"+ address);
                        OperateResult<short[]> operateResultShort = allenBradleyNet.ReadInt16(address, length);
                        
                        Console.WriteLine("operateResultShort:" + operateResultShort.Message);
                        if (operateResultShort.ErrorCode != 0)
                        {
                            Console.WriteLine("operateResultShort.Msg:"+ operateResultShort.ErrorCode);
                        }
                        foreach (short item in operateResultShort.Content)
                        {
                            SetValue(item);
                        }
                        
                    }
                    catch(Exception ex)
                    {
                        Console.WriteLine($"读取short类型数据错误{ex.Message}");
                    }
                    break;
                case Type.Long:
                    OperateResult<long[]> operateResultLong = allenBradleyNet.ReadInt64(address, length);
                    foreach (long item in operateResultLong.Content)
                    {
                        SetValue(item);
                    }
                    break;
                case Type.Float:
                    OperateResult<float[]> operateResultFloat = allenBradleyNet.ReadFloat(address, length);
                    foreach (float item in operateResultFloat.Content)
                    {
                        SetValue(item);
                    }
                    break;
                case Type.Double:
                    OperateResult<double[]> operateResultDouble = allenBradleyNet.ReadDouble(address, length);
                    foreach (double item in operateResultDouble.Content)
                    {
                        SetValue(item);
                    }
                    break;
                case Type.Bool: OperateResult<bool> operateResultBool = allenBradleyNet.ReadBool(address);
                    SetValue(operateResultBool.Content);
                    break;
                case Type.String:
                    OperateResult<string> operateResultString = allenBradleyNet.ReadString(address, length);
                    SetValue(operateResultString.Content);
                    break;
            }
        }

        #region 给richtext赋值
        public void SetValue(object o)
        {
            richTextBox.AppendText(o.ToString());
            richTextBox.AppendText(" ");
        }
        #endregion

        #region 选择数据类型
        private void rbShort_Checked(object sender, RoutedEventArgs e)
        {
            DataType = Type.Short;
        }

        private void rbInt_Checked(object sender, RoutedEventArgs e)
        {
            DataType = Type.Int;
        }

        private void rbLong_Checked(object sender, RoutedEventArgs e)
        {
            DataType = Type.Long;
        }

        private void rbBool_Checked(object sender, RoutedEventArgs e)
        {
            DataType = Type.Bool;
        }

        private void rbFloat_Checked(object sender, RoutedEventArgs e)
        {
            DataType = Type.Float;
        }

        private void rbDouble_Checked(object sender, RoutedEventArgs e)
        {
            DataType = Type.Double;
        }

        private void rbString_Checked(object sender, RoutedEventArgs e)
        {
            DataType = Type.String;
        }
        #endregion

        private void btnAB_Write__Click(object sender, RoutedEventArgs e)
        {
            if (allenBradleyNet == null)
            {
                return;
            }
            string address = txtPoint.Text;
            Console.WriteLine(address);
            ushort length = ushort.Parse(txtLength.Text);
            string value = this.txtABValue_.Text;
            switch (DataType)
            {
                case Type.None: break;
                case Type.Int:
                    OperateResult operateResultInt = allenBradleyNet.Write(address, int.Parse(value));
                    if (operateResultInt.IsSuccess)
                    {
                        MessageBox.Show("写入成功");
                    }
                    break;
                case Type.Short:
                    OperateResult operateResultShort = allenBradleyNet.Write(address, short.Parse(value));
                    if (operateResultShort.IsSuccess)
                    {
                        MessageBox.Show("写入成功");
                    }
                    break;
                case Type.Long:
                    OperateResult<long[]> operateResultLong = allenBradleyNet.ReadInt64(address, length);
                    foreach (long item in operateResultLong.Content)
                    {
                        SetValue(item);
                    }
                    break;
                case Type.Float:
                    OperateResult operateResultFloat = allenBradleyNet.Write(address, float.Parse(value));
                    if (operateResultFloat.IsSuccess)
                    {
                        MessageBox.Show("写入成功");
                    }
                    break;
                case Type.Double:
                    OperateResult<double[]> operateResultDouble = allenBradleyNet.ReadDouble(address, length);
                    foreach (double item in operateResultDouble.Content)
                    {
                        SetValue(item);
                    }
                    break;
                case Type.Bool:
                    bool b = false;
                    if(value == "1")
                    {
                        b = true;
                    }
                    OperateResult operateResultBoolt = allenBradleyNet.Write(address, b);
                    if (operateResultBoolt.IsSuccess)
                    {
                        MessageBox.Show("写入成功");
                    }
                    break;
                case Type.String:
                    OperateResult operateResultString = allenBradleyNet.Write(address, value);
                    if (operateResultString.IsSuccess)
                    {
                        MessageBox.Show("写入成功");
                    }
                    break;
            }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            siemensClient = new SiemensClient(SiemensVersion.S7_1500, this.textBox2.Text,102,0,0);
            OperateResult result = siemensClient.Open();
            if (!result.IsSuccess)
            {
                Console.WriteLine("PLC连接失败!" + result.Message);
            }
            else
            {
                MessageBox.Show("连接成功");
            }
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {

            Authorization authorization = new Authorization();
            

            richTextBox1.Document.Blocks.Clear();
            if (siemensClient == null)
            {
                return;
            }
            string address = txtPoint1.Text;
            Console.WriteLine(address);
            ushort length = ushort.Parse(txtLength1.Text);


            switch (DataType)
            {
                case Type.None: break;
                case Type.Int:
                    OperateResult<int> operateResultInt = siemensClient.ReadInt32(address);
                    
                        SetValue(operateResultInt.Content);
                    
                    break;
                case Type.Short:
                    try
                    {
                        Console.WriteLine("address:" + address);
                        OperateResult<short> operateResultShort = siemensClient.ReadInt16(address);

                        Console.WriteLine("operateResultShort:" + operateResultShort.Message);
                        if (operateResultShort.ErrorCode != 0)
                        {
                            Console.WriteLine("operateResultShort.Msg:" + operateResultShort.ErrorCode);
                        }
                        //foreach (short item in operateResultShort.Content)
                        {
                            SetValue(operateResultShort.Content);
                        }

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"读取short类型数据错误{ex.Message}");
                    }
                    break;
                case Type.Long:
                    OperateResult<long> operateResultLong = siemensClient.ReadInt64(address);
                    //foreach (long item in operateResultLong.Content)
                    {
                        SetValue(operateResultLong.Content);
                    }
                    break;
                case Type.Float:
                    try
                    {
                        OperateResult<float> operateResultFloat = siemensClient.ReadFloat(address);
                        //foreach (float item in operateResultFloat.Content)
                        SetValue(operateResultFloat.Content);
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    break;
                case Type.Double:
                    OperateResult<double> operateResultDouble = siemensClient.ReadDouble(address);
                    //foreach (double item in operateResultDouble.Content)
                    {
                        SetValue(operateResultDouble.Content);
                    }
                    break;
                case Type.Bool:
                    OperateResult<bool> operateResultBool = siemensClient.ReadBool(address);
                    SetValue(operateResultBool.Content);
                    break;
                case Type.String:
                    try
                    {
                        OperateResult<string> operateResultString = siemensClient.ReadString(address, length);
                        SetValue(operateResultString.Content);
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    
                    break;
            }
        }

        private void btnAB_Write_1_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnAB_Write_1_Click_1(object sender, RoutedEventArgs e)
        {
            if (siemensClient == null)
            {
                return;
            }
            string address = txtPoint1.Text;
            Console.WriteLine(address);
            ushort length = ushort.Parse(txtLength1.Text);
            string value = this.txtABValue_1.Text;

            switch (DataType)
            {
                case Type.None: break;
                case Type.Int:
                    OperateResult operateResultInt = siemensClient.Write(address, int.Parse(value));
                    if (operateResultInt.IsSuccess)
                    {
                        MessageBox.Show("写入成功");
                    }
                    break;
                case Type.Short:
                    OperateResult operateResultShort = siemensClient.Write(address, short.Parse(value));
                    if (operateResultShort.IsSuccess)
                    {
                        MessageBox.Show("写入成功");
                    }
                    break;
                case Type.Long:
                    OperateResult<long[]> operateResultLong = siemensClient.ReadInt64(address, length);
                    foreach (long item in operateResultLong.Content)
                    {
                        SetValue(item);
                    }
                    break;
                case Type.Float:
                    OperateResult operateResultFloat = siemensClient.Write(address, float.Parse(value));
                    if (operateResultFloat.IsSuccess)
                    {
                        MessageBox.Show("写入成功");
                    }
                    break;
                case Type.Double:
                    OperateResult<double[]> operateResultDouble = siemensClient.ReadDouble(address, length);
                    foreach (double item in operateResultDouble.Content)
                    {
                        SetValue(item);
                    }
                    break;
                case Type.Bool:
                    bool b = false;
                    if (value == "1")
                    {
                        b = true;
                    }
                    OperateResult operateResultBoolt = siemensClient.Write(address, b);
                    if (operateResultBoolt.IsSuccess)
                    {
                        MessageBox.Show("写入成功");
                    }
                    break;
                case Type.String:
                    OperateResult operateResultString = siemensClient.Write(address, value,length);
                    if (operateResultString.IsSuccess)
                    {
                        MessageBox.Show("写入成功");
                    }
                    break;
            }
        }
    }

    public enum Type
    {
        None = -1,
        Short = 0,
        Int = 1,
        Long = 2,
        Float = 3,
        Double = 4,
        String = 5,
        Bool = 6
    }
}